package com.example.hello;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import androidx.drawerlayout.widget.DrawerLayout;

public class RateManager {
    private static final String TAG = "RateManager";
    private DBHelper dbHelper;
    private String TBNAME;

    public RateManager(Context context) {
        dbHelper=new DBHelper(context);
        TBNAME=DBHelper.TB_NAME;
    }
    public void add(RateItem item){
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();//跟RateItem能互换么？
        values.put("curname",item.getCurName());
        values.put("currate",item.getCurRate());
        long r=db.insert(TBNAME,null,values);
        Log.i(TAG,"add:ret="+r);//r：行id值
        db.close();
    }
    public void addAll(List<RateItem> list){
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        for(RateItem item:list){
            ContentValues values=new ContentValues();//跟RateItem能互换么？
            values.put("curname",item.getCurName());
            values.put("currate",item.getCurRate());
            db.insert(TBNAME,null,values);
            //Log.i(TAG,"add:ret="+r);//r：行id值
        }

        db.close();
    }

    public void deleteAll(){
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        db.delete(TBNAME,null,null);
        db.close();
    }
    public  void delete(int id){
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        db.delete(TBNAME,"id=?",new String[]{String.valueOf(id)});
        db.close();
    }

    public  void update(RateItem item){
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("cuename",item.getCurName());
        values.put("cuerate",item.getCurRate());
        db.update(TBNAME,values,"id=?",new String[]{String.valueOf(item.getId())});
        db.close();


    }

    public List<RateItem> listAll(){
        List<RateItem> ret=new ArrayList<RateItem>();
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        Cursor cursor=db.query(TBNAME,null,null,
                null,null,null,null);
        if(cursor!=null){
            while(cursor.moveToNext()){
                RateItem item=new RateItem();
                item.setId(cursor.getInt(cursor.getColumnIndex("id")));
                item.setCurName(cursor.getString(cursor.getColumnIndex("curname")));
                item.setCurRate(cursor.getString(cursor.getColumnIndex("currate")));
                ret.add(item);
            }
            cursor.close();

        }
        db.close();
        return ret;
    }

    public RateItem findById(int id){
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        Cursor cursor=db.query(TBNAME,null,"id=?",new String[]{String.valueOf(id)},null,null,null);
        RateItem rateItem=null;
        if(cursor!=null&&cursor.moveToFirst()){
            rateItem=new RateItem();
            rateItem.setId(cursor.getInt(cursor.getColumnIndex("id")));
            rateItem.setCurRate(cursor.getString(cursor.getColumnIndex("currate")));
            rateItem.setCurName(cursor.getString(cursor.getColumnIndex("curname")));
        }
        cursor.close();
        db.close();
        return rateItem;
    }

    public RateItem findByName(String name,String rate){
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        Cursor cursor=db.query(TBNAME,null,"name=? or rate=?",new String[]{name,rate},null,null,null);
        RateItem rateItem=null;
        if(cursor!=null&&cursor.moveToFirst()){
            rateItem=new RateItem();
            rateItem.setId(cursor.getInt(cursor.getColumnIndex("id")));
            rateItem.setCurRate(cursor.getString(cursor.getColumnIndex("currate")));
            rateItem.setCurName(cursor.getString(cursor.getColumnIndex("curname")));
        }
        cursor.close();
        db.close();
        return rateItem;
    }



}
